<?php
/**
 * 商家管理
 * @author Administrator
 *
 */
class shopManage extends CControllerManageBase{
	public function show()
	{
		$model = new shopModel();
		$pageIndex = $this->getRequestData("page","GET",1);
		$pageSize = 10;
		$c = 0;
		$show_status = $this->getRequestData("sel_shop_status","get");
		$province = $this->getRequestData("sel_province","get");
		$city = $this->getRequestData("sel_city","get");
		$area = $this->getRequestData("sel_area","get");
		$shop_name = $this->getRequestData("txt_shop_name","get");
		
		$urlParam = array();
		$where = "t.del_flag=0";
		
		if($province)
		{
			$where .= " and province='".$province."'";
			
			$urlParam["sel_province"] = $province;
		}
		
		if($city)
		{
			$where .= " and city='".$city."'";
			
			$urlParam["sel_city"] = $city;
		}
		
		if($area)
		{
			$where .= " and area='".$area."'";
			
			$urlParam["sel_area"] = $area;
		}
		
		if($show_status)
		{
			$where .= " and shop_status=".$show_status."";
			
			$urlParam["sel_shop_status"] = $show_status;
		}
		
		if($shop_name)
		{
			$where .= " and (shop_name like '%".$shop_name."%' or shop_simple_name like '%".$shop_name."%')";
			
			$urlParam["txt_shop_name"] = $shop_name;
		}
		
		$this->setViewParam("shop_name", $shop_name);
		
		$pageUrl = $this->getUrlParam($urlParam, array("txt_shop_name"));
		$pageUrl = $pageUrl? "?".$pageUrl:"";
		
		$list = $model->selectPage($pageIndex,$pageSize,"t.*",$where,"shop_id desc","",$c,"t_shop t");

		$pageStr = CPage::showPage($pageIndex,$pageSize,$c,$pageUrl);

		if($pageUrl)
		{
			$pageUrl .= "&page=".$pageSize;
		}
		else{
			$pageUrl .= "?page=".$pageSize;
		}
		$this->setJsBlock("init('".$province."','".$city."','".$area."',1);");
		$this->setMenu("商家列表");
		$this->render("shop/list",array("list"=>$list,"pageUrl"=>$pageUrl,"pageStr"=>$pageStr,"show_status"=>$show_status));
	}
	
	private function getPostInfo()
	{
		$modelInfo = array();
		if(!isset($_GET["id"]))
		{
			$modelInfo["user_name"] = $this->getRequestData("txt_user_name");
			$modelInfo["create_time"] = date("Y-m-d H:i:s");
		}
		$modelInfo["shop_name"] = $this->getRequestData("txt_shop_name");
		$modelInfo["phone"] = $this->getRequestData("txt_phone");
		$modelInfo["shop_simple_name"] = $this->getRequestData("txt_shop_simple_name");
		$modelInfo["back_account"] = $this->getRequestData("txt_back_account");
		$modelInfo["contact"] = $this->getRequestData("txt_contact");
		$modelInfo["mail"] = $this->getRequestData("txt_mail");
		$modelInfo["qq"] = $this->getRequestData("txt_qq");
		$modelInfo["bus_content"] = $this->getRequestData("txt_bus_content");
		//$modelInfo["remark"] = $this->getRequestData("txt_remark");
		$modelInfo["bus_license_pic"] = $this->getRequestData("hidImgPath");
		$modelInfo["shop_address"] = $this->getRequestData("txt_address");
		
		return $modelInfo;
	}
	
	public function add(){
		$errorStr="";
		$rule=CCustomFormValidator::shopValidator();
		$this->formValidator->init(array("formID"=>"t_shopForm"));
		$this->formValidator->setRules($rule);
		if($_POST){
			$modelInfo = $this->getPostInfo();
			
			$modelInfo["user_pwd"] = CCommon::pwdMd5("000000");
			$error = $this->formValidator->run();
			if($error){
				$errorStr = "<div><ul>";
				foreach ($error as $item){
					$errorStr .= "<li>".$item."</li>";
				}
				$errorStr .= "</ul></div>";
			}
			else{
				$model=new shopModel();
				$model->db->beginTransaction();
				$sid=$model->insert($modelInfo);

				$shop_desc = $this->getRequestData('txt_content',"POST","",false);
				$model->insertShopDesc($sid,$shop_desc);
				
				if($sid>0)
				{
					$clist = $this->getRequestData("hidSelCommunityID"); //管理的社区
			        
					if($clist)
					{
						$clist = explode(",", $clist);
						foreach($clist as $cid)
						{
							$model->addShopCommunity($sid, $cid);
						}
					}
					
					$scInfo["shop_id"] = $sid;
					$scInfo["p_name"] = $modelInfo["shop_name"];
					$scInfo["pic_path"] = "/images/o_list_demo.jpg";
					$scInfo["p_type"] = 1;
					$scInfo["create_time"] = date("Y-m-d H:i:s");
					
					$scModel = new shopProductModel();
					$scModel->insert($scInfo);
					$model->db->commit();
					
					$this->sysMsg("新增成功",0,array(array("text"=>"返回商家列表","href"=>$this->_manageDomain."shopManage/show/"),array("text"=>"继续新增","href"=>$this->_manageDomain."shopManage/add/")));
				}
				else
				{
					$this->sysMsg("新增失败",1);
				}
			}
		}

		$this->setStyleFile("/css/validator.css");
		$this->setJsFile(array("/js/formValidator.js","/js/formValidatorRegex.js","/kindeditor/kindeditor-min.js", "/kindeditor/lang/zh_CN.js"));
        $this->setJsBlock("init('','','',1);_changeSelCallBack=true;_mutiCommunity=true;var editor;
        KindEditor.ready(function(K) {
                editor = K.create('#txt_content');
        });");
		$this->setMenu(array(array("text"=>"商家列表","href"=>$this->_manageDomain."shopManage/show/"),array("text"=>"商家新增")));
		$this->render("shop/add",array("error"=>$errorStr));
	}

	public function edit(){
		$shop_id = $this->getRequestData("id","GET");
		$errorStr = "";

		$rule=CCustomFormValidator::shopValidator($shop_id);
		$this->formValidator->init(array("formID"=>"t_shopForm"));
		$this->formValidator->setRules($rule);

		$model=new shopModel();
		if($_POST){
			$modelInfo = $this->getPostInfo();
			$modelInfo["shop_id"] = $shop_id;
			
			$error = $this->formValidator->run();
			if($error){
				$errorStr = "<div><ul>";
				foreach ($error as $item){
					$errorStr .= "<li>".$item."</li>";
				}
				$errorStr .= "</ul></div>";
			}
			else{
				$model->db->beginTransaction();
				$model->updateByPkId($modelInfo);
				
				$shop_desc = $this->getRequestData('txt_content',"POST","",false);

				$descIsExist = $model->getCountByWhere("shop_id=".$shop_id,"t_shop_desc");
				if($descIsExist)
					$model->updateShopDesc($shop_id, $shop_desc);
				else{
					$model->insertShopDesc($shop_id,$shop_desc);
				}
				
				$model->delShopCommunity($shop_id);
				
				$clist = $this->getRequestData("hidSelCommunityID"); //管理的社区
				if($clist)
				{
					$clist = explode(",", $clist);
					foreach($clist as $cid)
					{
						$model->addShopCommunity($shop_id, $cid);
					}
				}
				$model->db->commit();
		        header("location:".$this->_manageDomain."shopManage/show/");
		        exit();
			}
		}
		else{
			$modelInfo = $model->selectById($shop_id);
			if(!$modelInfo) {
				$this->sysMsg("记录不存在",1);
			}

			$c = $model->getShopDesc($shop_id);
			$content = "";
			if($c && is_array($c) && isset($c["shop_desc"]))
			{
				$content = stripslashes($c["shop_desc"]);
			}
			$modelInfo["shop_desc"] = $content;
			
			$clist = $model->getShopCommunity($shop_id);
			
			$selName = "";
			$selID = "";
			
			foreach ($clist as $item)
			{
				if($selName)
				{
					$selName .= ",";
					$selID .= ",";
				}

				$selName .= $item["community_name"];
				$selID .= $item["community_id"];
			}
	        
	        $this->setViewParam("selName",$selName);
	        $this->setViewParam("selID",$selID);
		}
		
		$this->setStyleFile("/css/validator.css");
		$this->setJsFile(array("/js/formValidator.js","/js/formValidatorRegex.js","/kindeditor/kindeditor-min.js", "/kindeditor/lang/zh_CN.js"));
        $this->setJsBlock("init('','','',1);_changeSelCallBack=true;_mutiCommunity=true;var editor;
        KindEditor.ready(function(K) {
                editor = K.create('#txt_content');
        });");
		
		$this->setMenu(array(array("text"=>"商家列表","href"=>$this->_manageDomain."shopManage/show/"),array("text"=>"商家修改")));
		$this->render("shop/edit",array("error"=>$errorStr,"modelInfo"=>$modelInfo));
	}
	
	public function del()
	{
		$id = $this->getRequestData("id","GET");
		if ($id)
        {
            $buc = new shopModel();

			$buc->deleteByPkId($id);
            $this->show();
        }
	}
	
	public function batchDel()
	{
		$ids = $this->getRequestData("ids","GET");
		
		$idsList = explode(",",$ids);
		
		$buc = new shopModel();
		foreach($idsList as $id)
		{
            $buc->deleteByPkId($id);
		}
		$this->show();
	}
	
	
	public function changeStatus()
	{
		$id = $this->getRequestData("id","GET");
		$type = $this->getRequestData("type","GET");
		
		if($type!=1 && $type!=2)
		{
			echo "error";
			exit;
		}
		
		$model = new shopModel();
		
		$shopInfo["shop_id"] = $id;
		$shopInfo["shop_status"] = $type;
		
		$model->updateByPkId($shopInfo);
		
		$this->show();
	}
	
	/**
	 * 验证登录名是否存在
	 */
	public function checkUserName()
	{
		$f = new CCustomFormValidator();
		$id = $this->getRequestData("id","get");
		echo $f->checkShopUserName($this->getRequestData("txt_user_name","get"),$id)?1:0;
		exit;
	}
	
	public function resetUserPwd()
	{
		$this->resetPwd("user_pwd");
	}
	
	public function resetPayPwd()
	{
		$this->resetPwd("pay_pwd");
	}
	
	public function resetPwd($type)
	{
		$shopInfo["shop_id"] = $this->getRequestData("id");
		$shopInfo[$type] = CCommon::pwdMd5("000000");
		$shopModel = new shopModel();

		echo $shopModel->updateByPkId($shopInfo);
		exit;
	}
}